package org.dentalcard.ui.schedule.tools;

import org.dentalcard.entity.Shift;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.AbstractDecTableModel;
import org.dentalcard.ui.main.MainFrameBuilder;
import sun.util.resources.CalendarData;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 11:23
 */
public class WorkDayModel extends AbstractDecTableModel {

    private DateFormat dateFormat = DateFormat.getDateInstance();
    private Calendar calendar = GregorianCalendar.getInstance();
    private static String[] days = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
    private ScheduleDaoImpl shiftDao;
    private List<WorkDay> workDay;

    public WorkDayModel() {
        super(new String[]{"Date", "Day", "Shift name"});
        shiftDao = new ScheduleDaoImpl();
        reload();
    }

    public void reload() {
        workDay = shiftDao.getWorkDayList(MainFrameBuilder.getInstance().getDecUser());
        fireTableDataChanged();
    }


    @Override
    public int getRowCount() {
        if(workDay.isEmpty()){
            return 1;
        }
        return workDay.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(workDay.isEmpty()){
            return "";
        }
        WorkDay day = workDay.get(rowIndex);
        switch (columnIndex) {
            case 0 :{
                if(day.getDay() == null){
                    return "";
                }
                return dateFormat.format(day.getDay());
            }
            case 1 :{
                if(day.getDay() == null){
                    return "";
                }
                calendar.setTime(day.getDay());
                int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                return days[dayOfWeek - 1];
            }
            case 2:{
                return day.getShift().getName();
            }
        }
        return "";

    }

    public WorkDay getWorkDay(int selectedRow) {
        if(workDay.isEmpty()){
            return null;
        }
        return workDay.get(selectedRow);
    }
}
